package com.tupi.project.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

import com.github.moduth.blockcanary.BlockCanary;
import com.squareup.leakcanary.LeakCanary;
import com.tupi.project.Service.InitializeService;
import com.tupi.project.di.component.AppComponent;
import com.tupi.project.di.component.DaggerAppComponent;
import com.tupi.project.di.module.AppModule;
import com.tupi.project.di.module.HttpModule;
import com.tupi.project.component.AppBlockCanaryContext;

import java.util.HashSet;
import java.util.Set;

/**
 * BaseApp
 * Created by CPP on 2017/8/15.
 */

public class App extends Application
{
    public static AppComponent appComponent;
    
    private Set<Activity> allActivities;
    
    public static int SCREEN_WIDTH = -1;
    public static int SCREEN_HEIGHT = -1;
    public static float DIMEN_RATE = -1.0F;
    public static int DIMEN_DPI = -1;
    private static App instance;
    
    public static synchronized App getInstance()
    {
        return instance;
    }
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        instance = this;
        
        //初始化屏幕宽高
        getScreenSize();
        
        //在子线程中完成其他初始化
        InitializeService.start(this);
        
        //初始化内存泄漏检测
        LeakCanary.install(App.getInstance());
        
        //初始化过度绘制检测
        BlockCanary.install(getApplicationContext(), new AppBlockCanaryContext()).start();
        
    }
    
    protected void attachBaseContext(Context base)
    {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    
    public void getScreenSize()
    {
        WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(dm);
        DIMEN_RATE = dm.density / 1.0F;
        DIMEN_DPI = dm.densityDpi;
        SCREEN_WIDTH = dm.widthPixels;
        SCREEN_HEIGHT = dm.heightPixels;
        if (SCREEN_WIDTH > SCREEN_HEIGHT)
        {
            int t = SCREEN_HEIGHT;
            SCREEN_HEIGHT = SCREEN_WIDTH;
            SCREEN_WIDTH = t;
        }
    }
    
    public void addActivity(Activity act)
    {
        if (allActivities == null)
        {
            allActivities = new HashSet<>();
        }
        allActivities.add(act);
    }
    
    public void removeActivity(Activity act)
    {
        if (allActivities != null)
        {
            allActivities.remove(act);
        }
    }
    
    public void exitApp()
    {
        if (allActivities != null)
        {
            synchronized (allActivities)
            {
                for (Activity act : allActivities)
                {
                    act.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
    
    public static AppComponent getAppComponent()
    {
        if (appComponent == null)
        {
            appComponent = DaggerAppComponent.builder().appModule(new AppModule(instance))
                    .httpModule(new HttpModule()).build();
        }
        return appComponent;
    }
}
